//
//  HSYLoginViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/5.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "HSYLoginViewController.h"
#import "HSYRegisterViewController.h"
#import "HSYForgetPasswordViewController.h"
#import "NSString+Validation.h"
#import "BQActivityView.h"
#import "HSYLoginRequest.h"

@interface HSYLoginViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *accountICONImageView;
@property (weak, nonatomic) IBOutlet UIImageView *passwordICONImageView;

@property (weak, nonatomic) IBOutlet UITextField *accountTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextFiled;
@property (weak, nonatomic) IBOutlet UIButton *encryptBtn;

@end

@implementation HSYLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.titleButton setTitle:@"登录" forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}


-(void)handleGoBackEvent:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (IBAction)registerBtnClick:(UIButton *)sender {
    [self.view endEditing:YES];
    
    HSYRegisterViewController *vc = [[HSYRegisterViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

//密码可见与否
- (IBAction)encryptBtnClick:(UIButton *)sender {
    if (self.passwordTextFiled.secureTextEntry) {
        [self.encryptBtn setImage:[UIImage imageNamed:@"dispaly"] forState:UIControlStateNormal];
            self.passwordTextFiled.secureTextEntry = NO;
    }else{
        [self.encryptBtn setImage:[UIImage imageNamed:@"encrypt"] forState:UIControlStateNormal];
        self.passwordTextFiled.secureTextEntry = YES;
    }
}

//忘记密码
- (IBAction)forgotPWDBtnClcik:(id)sender {
    HSYForgetPasswordViewController *vc = [[HSYForgetPasswordViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

//登录
- (IBAction)loginBtnClcik:(UIButton *)sender {
    [self.view endEditing:YES];
    
    if (!self.accountTextFiled.text.length) {
        [self showMessage:@"请输入手机号"];
        return ;
    }
    
    if (![self.accountTextFiled.text isValidPhone]) {
        [self showMessage:@"请输入正确的手机号"];
        return ;
    }
    
    if (!self.passwordTextFiled.text.length) {
        [self showMessage:@"请输入密码"];
        return ;
    }
    
    if (self.passwordTextFiled.text.length<6) {
        [self showMessage:@"请输入大于等于6位的密码"];
        return ;
    }
    
    if (self.passwordTextFiled.text.length>12) {
        [self showMessage:@"请输入小于等于12位的密码"];
        return ;
    }
    
    //发起请求
    [BQActivityView showActiviTy];
    __weak typeof(self)weakSelf = self;

    HSYLoginRequest *task = [[HSYLoginRequest alloc] init];
    task.cellphone = self.accountTextFiled.text;
    task.password = self.passwordTextFiled.text;
    
    [HSYNetworkManager POST:task success:^(HSYResponse *response) {
        [BQActivityView hideActiviTy];
        
        if (response.resultCode == HSY_REQUEST_CODE_SUCCESS) {
            [APPCenter defaultCenter].password = weakSelf.passwordTextFiled.text;
            [APPCenter defaultCenter].username = weakSelf.accountTextFiled.text;
            //获取用户信息并储存
            HSYUserCenter *userCenter = [HSYUserCenter mj_objectWithKeyValues:response.encryptDataResult];
            [userCenter saveUserModelToDocuments];

            //返回
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        }else{
            [weakSelf showMessage:response.resultMessage];
        }
    } failure:^(NSError *error) {
        [BQActivityView hideActiviTy];
        if (error.code != NSURLErrorCancelled) {
            [weakSelf showMessage:@"请求超时"];
        }
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
